package com.atguigu.chapter07;

import com.atguigu.chapter05.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/3/6 9:12
 */
public class Flink16_Watermark_SideOutput {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        SingleOutputStreamOperator<WaterSensor> sensorDS = env
                .socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        // 切分
                        String[] line = value.split(",");
                        return new WaterSensor(line[0], Long.valueOf(line[1]), Integer.valueOf(line[2]));

                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()      // 指定watermark的生成
                                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                                    @Override
                                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                                        return element.getTs() * 1000L; // Flink时间戳单位都是 毫秒
                                    }
                                })

                );


        KeyedStream<WaterSensor, String> sensorKS = sensorDS.keyBy(sensor -> sensor.getId());

        // 分组之后开窗：
        OutputTag<WaterSensor> outputTag = new OutputTag<WaterSensor>("late") {
        };

        WindowedStream<WaterSensor, String, TimeWindow> sensorWS = sensorKS
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .sideOutputLateData(outputTag);


        SingleOutputStreamOperator<String> resultDS = sensorWS
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String key, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        out.collect("key=" + key + "\n" +
                                "数据条数:" + elements.spliterator().estimateSize() + "\n" +
                                "窗口是:[" + context.window().getStart() + "," + context.window().getEnd() + ")\n\n");
                    }
                });

        resultDS.print("result");

        // TODO 获取侧输出流
        DataStream<WaterSensor> sideOutput = resultDS.getSideOutput(outputTag);
        sideOutput.print("late");

        // TODO 汇总迟到数据到主流: 两条流合并
//        resultDS.connect(sideOutput).process();


        env.execute();
    }
}

/*
    迟到数据： 当前数据的时间戳 小于 当前的 watermark
    窗口的迟到数据：end之后，才来的 属于本窗口的数据
    窗口允许迟到：
        1） 当 watermark >= end - 1 时， 触发窗口计算
        2） 当 end - 1ms < watermark < end + 允许迟到时间 -1 ms 时，每来一条迟到数据，都会触发一次计算
        3） 当 watermark >= end + 允许迟到时间 -1 ms, 就会 关闭窗口，再有迟到的数据来，也不处理了
 */